<!DOCTYPE html>
<html lang="en">
<head>
    <div th:include="base-libs :: base-libs" />
    <script th:src="@{/service/js/JsBarcode.all.min.js}"></script>
    <meta charset="UTF-8">
    <title>确定协议</title>
    <style>
        .fwbSpan{
            font-weight: 800;
            color: blue;
            cursor: pointer;
        }

    </style>
    <script th:inline="javascript">
        /**读取request中的初始化数据*/
        var requestData = [[${requestData}]];
    </script>
    <script type="text/javascript">
        var BACKINFO = StoreCache.removeCache("QYXYBACKINFO");//跳转页面
        var SFXY = StoreCache.removeCache("SFXY");
        var xyInitOver = false; //协议加载完成
        $(function() {
            $(this).registerManagerPage({
                cacheTables: ["TB_ZY_XB"]
                ,setCacheTableData: false
                ,addNotNullClass: false
                ,autoInitQuickField: false
                ,callback: initView
            });
        });

        function initView(cacheData){
            $("#btnQd").on("click", btnQdHandler);
            $("#btnQx").on("click", btnQxHandler);
            $("body").on("click",".fwbSpan",queryFwbxx)

            initData(cacheData);

        }

        function initData(cacheData){
            var qyxx = StoreCache.removeCache("xzqy_qyxx");
            $("body").data("qyxx", qyxx);

            $.QuickRemote.AjaxJson("queryUseJtqyxy", function(result){
                if(result.state){
                    var content = result.info["CONTENT"];
                    $("#xynrDiv").append(content);
                    //协议号
                    $("#XYH").html(result.info["XYH"]);
                    /*条形码设置*/
                    /*$("#XYH").attr("title",result.info["XYH"]);
                    var JsBarcode_obj = {
                        format: "CODE128",//选择要使用的条形码类型
                            width:6,//设置条之间的宽度
                        height:100,//高度,单位：%
                        displayValue:false,//是否在条形码下方显示文字
                        font:"fantasy",//设置文本的字体
                        textAlign:"center",//设置文本的水平对齐方式
                        textPosition:"top",//设置文本的垂直位置
                        textMargin:5,//设置条形码和文本之间的间距
                        fontSize:25,//设置文本的大小
                    }
                    $("#txmBox").JsBarcode(result.info["XYH"], JsBarcode_obj);//or JsBarcode("#svgcode", "Hi!");*/


                    //填充所有数据
                    fillViewData();
                    //操作信息表格处理
                    czxxDivFormat();

                }else{
                    $.QuickAlert.alertFail({content: result.info.faultInfo});
                }
            }, null);
        }

        /** 填充所有数据 */
        function fillViewData(){
            var qyxx = $("body").data("qyxx");
//            alert(JSON.stringify(qyxx))
            if(qyxx){
                var xyModelData = {};
                /*乙方信息*/
                var JMXX = qyxx.JMXX;
                /*甲方信息*/
                requestData.YFXM = qyxx.YFXM;
                requestData.YFSJHM = qyxx.YFSJHM;

                var fwbmcAry = qyxx["QYFWBMC"];
                var fwbId = [],fwbMc = [];
                $.each(fwbmcAry, function(index, item){
                    var span = $("<span>").addClass("fwbSpan").data("FWBID",item.ID).html(function(){
                        var back = item["MC"];
                        if(index != fwbmcAry.length-1) back += ";";
                        return back;
                    });
                    $("#FWBMC").append(span);
                    fwbId.push(item.ID);
                    fwbMc.push(item.MC)
                })
                xyModelData = $.extend({FWBIDS:fwbId.join("|")},JMXX,requestData);

                /* 显示续约信息 */
                if(!StringUtils.isEmpty(SFXY)){
                    var xyxxObj = $.extend({},qyxx.QYXX,requestData);
                    xyxxObj.FWBMC = fwbMc.join(";");
                    xyxxObj.XYSJ = new Date().Format('yyyy-MM-dd');
                    xyxxObj.QYTDMC = qyxx.QYTDMC;
                    xyModelData = $.extend(xyModelData,xyxxObj);
                }
//                console.log(JSON.stringify(xyModelData))

                xyModelData.SXSJ = qyxx.QYXX.SXSJ;
                xyModelData.JSSJ = qyxx.QYXX.JSSJ;
                xyModelData.YBFWX_ARR = qyxx.YBFWX_ARR;
                XymbMethod.initXymbData(xyModelData);
                //载入服务包价格
                initFwbPrice(fwbId.join("|"));
            }
        }

        /** 载入服务包价格 */
        function initFwbPrice(FWBIDS){
            if(!StringUtils.isEmpty(FWBIDS)){
                $.QuickRemote.AjaxJson(contextRoot + "jtqy/queryXyFWB.service", function(result){
                    var obj = {FY:0,GWJFZF:0,YBGRZF:0,JMZFFY:0};
                    $.each(result, function(i,item){
                        obj.FY = obj.FY + parseFloat(this.FY || 0);
                        obj.GWJFZF = obj.GWJFZF + parseFloat(this.GWJFZF || 0);
                        obj.YBGRZF = obj.YBGRZF + parseFloat(this.YBGRZF || 0);
                        obj.JMZFFY = obj.JMZFFY + parseFloat(this.JMZFFY || 0);
                    })
                    for(var key in obj){
                        $("[name="+key+"]").val(obj[key]);
                    }
                    xyInitOver = true;
                },{FWBIDS:FWBIDS});
            }
        }

        function btnQdHandler(){
            if(!xyInitOver) return;
            var formatQyxx = getFormatQyxx();

            XymbMethod.saveXymbAgo();

            var xyContent = $("#xynrDiv").html();
            if(StringUtils.isEmpty(xyContent)){
                $.QuickAlert.alertFail({content: "协议内容丢失，签约失败！"});
                return;
            }
            formatQyxx.CONTENT = xyContent.replace(/%/g,"gzbBfh").replace(/\+/g,"gzbJh");

            $.QuickRemote.AjaxJson("addQy", function(result){
                if (result["state"]) {
                    $.QuickAlert.alertNormal("签约成功！",{callback: function(){
                        StoreCache.removeCache("JTQYXYINFO");
                        StoreCache.removeCache("BACKINFO");
                        StoreCache.removeCache("XYBACKINFO");
                        /*if(BACKINFO.SUCCESS){
                            location = BACKINFO.SUCCESS;
                        }else{
                            parent.selectMenuByHand('正式签约');
                        }*/
                        StoreCache.setCache("toIndex",true);
                        $("#btnQx").trigger("click");
                    }});
                } else {
                    $.QuickAlert.alertFail({
                        content : result.info["faultInfo"]
                    });
                }
            }, formatQyxx);

        }

        /**格式化签约信息*/
        function getFormatQyxx(){
            var qyxx = $("body").data("qyxx");
            var formatQyxx = $.extend(true, {}, qyxx);

            formatQyxx.TDMC = formatQyxx["QYTDMC"];
            delete formatQyxx["YFTDMC"];
            delete formatQyxx["QYFWBMC"];
            /*家庭成员 删除签约信息字段*/
            var JTCY = formatQyxx["JTCY"];
            if(JTCY){
                $.each(JTCY, function(item){
                    delete this["QYXX"];
                })
            }
            /*格式化人群分类*/
            var RQFL = formatQyxx["RQFL"];
            var rqflStr = [];
            if(RQFL){
                $.each(RQFL, function(item){
                    rqflStr.push(this["RQFLID"]);
                })
            }
            /*添加签约费用*/
//            var QYFY = $("#QYFY").val();
//            formatQyxx["QYXX"]["QYFY"] = QYFY;
            formatQyxx["QYXX"]["YSXM"] = qyxx.QYYS;
            formatQyxx["RQFL"] = rqflStr.join(",");
            formatQyxx["JMXX"] = JSON.stringify(formatQyxx["JMXX"]);
            formatQyxx["QYXX"] = JSON.stringify(formatQyxx["QYXX"]);
            formatQyxx["JTCY"] = JSON.stringify(formatQyxx["JTCY"]);
            formatQyxx["XYH"] = $("#XYH").text();
            if(!StringUtils.isEmpty(SFXY)){ //是否正在续约(如果不是续约，则是新增签约)
                formatQyxx["action"] = "xyfw";
            }
            return formatQyxx;
        }

        function btnQxHandler(){
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        }

        /** 查询服务包详细  */
        function queryFwbxx(){
            var FWBID = $(this).data("FWBID");
            var FWBMC = $(this).text();
            //$.QuickUrlUtils.redirectUrl("detail.html?FWBID="+row["FWBID"]);
            parent.layer.open({
                type: 2,
                area: ["60%", "50%"],
                content: contextRoot + 'jtqy/jtqyXyFwbDetail.html?FWBID='+FWBID,
                scrollbar: false,
                resize: false,
                title: '<span style="color:blue">' + FWBMC + '</span>详细',
            });
        }

        /** 操作信息Div处理(删除||展现)  */
        function czxxDivFormat(){
            var czxxDivId = "";
            if(!StringUtils.isEmpty(SFXY)){ //是否正在续约
                czxxDivId = "xyxxDiv";
            }
            $(".czxxDiv").each(function(){
                if(this.id == czxxDivId){
                    $(this).parent().show(); //展示出来
                }else{
                    $(this).parent().remove(); //删除不需要的
                }
            })
        }
    </script>
</head>
<body class="easyui-layout" border="false">
<div region="center" border="false">
    <div  style="width:695px;margin:0 auto;" border="false" id="xynrDiv">
    </div>
</div>
<div region="south" style="height: 41px; border: none; overflow: hidden; background-color: #f9f9f9; padding-top: 6px; text-align: center;">
    <button id="btnQd" type="button" style=" margin-left: 10px;">确定</button>
    <button id="btnQx" type="button" style=" margin-left: 10px;">取消</button>
</div>
</body>
</html>